package com.bitsinharmony.recognito;

import com.bitsinharmony.recognito.MatchResult;
import com.bitsinharmony.recognito.Recognito;
import com.bitsinharmony.recognito.VoicePrint;

import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2021/4/13.
 */
public class VoiceController {
    public static void main(String[] args) {
        Recognito<String> recognito = new Recognito<>(48000.0f);

        VoicePrint print = null;
        VoicePrint print2 = null;
        try {
            print = recognito.createVoicePrint("p1", new File("C:\\Users\\Administrator\\Desktop\\6.wav"));
            //recognito.mergeVoiceSample("p1", new File("C:\\Users\\Administrator\\Desktop\\6.wav"));
            //recognito.mergeVoiceSample("p1", new File("C:\\Users\\Administrator\\Desktop\\7.wav"));
            recognito.mergeVoiceSample("p1", new File("C:\\Users\\Administrator\\Desktop\\7.wav"));
            //recognito.mergeVoiceSample("p1", new File("C:\\Users\\Administrator\\Desktop\\6.wav"));
            recognito.mergeVoiceSample("p1", new File("C:\\Users\\Administrator\\Desktop\\5.wav"));
            print2 = recognito.createVoicePrint("p2", new File("C:\\Users\\Administrator\\Desktop\\4.wav"));
            //recognito.mergeVoiceSample("p2", new File("C:\\Users\\Administrator\\Desktop\\3.wav"));
            //recognito.mergeVoiceSample("p2", new File("C:\\Users\\Administrator\\Desktop\\2.wav"));
            //recognito.mergeVoiceSample("p2", new File("C:\\Users\\Administrator\\Desktop\\1.wav"));
            //recognito.mergeVoiceSample("p2", new File("C:\\Users\\Administrator\\Desktop\\0.wav"));
            //recognito.mergeVoiceSample("p2", new File("C:\\Users\\Administrator\\Desktop\\4.wav"));
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(print);
        System.out.println(print2);
        // handle persistence the way you want, e.g.:
        // myUser.setVocalPrint(print);
        // userDao.saveOrUpdate(myUser);

        // Now check if the King is back
        List<MatchResult<String>> matches = null;
        while (true){
            File file  = new File("C:\\Users\\Administrator\\Desktop\\cache.wav");

            int temp = 1;
            while (true){
                if (!file.exists()){
                    if (temp == 1){
                        System.out.println("等待录音文件");
                        temp = 0;
                    }
                }else{
                    break;
                }
            }



            try {
                matches = recognito.identify(file);
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            MatchResult<String> match = matches.get(0);
            System.out.println(match.getKey() + "欢迎回来，" + match.getLikelihoodRatio() + "% positive about it...");

            //如果匹配度大于97就合并
            if(match.getLikelihoodRatio() >= 97) {
                try {
                    print = recognito.mergeVoiceSample(match.getKey(), file);
                    System.out.println(print);
                } catch (UnsupportedAudioFileException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(file.delete());
        }


    }
}
